package com.spring.test.factory;

import org.springframework.beans.factory.FactoryBean;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

/**
 * 实例工厂方法:实例工厂的方法，即先需要创建工厂本身，再调用工厂的实例方法来返回bean的实例
 */
public class InstanceCarFactory{
	private Map<String,Car> cars=null;
	private static Map<String,ElectricCar> electricCars=null;
	public InstanceCarFactory() {
		cars=new HashMap<String,Car>();
		cars.put("audi", new Car("audi",300000));
		cars.put("ford", new Car("ford",400000));
		electricCars=new HashMap<>();
		electricCars.put("bmw",new ElectricCar("bmw",500000));
	}

	public Car getCar(String brand) {
		return cars.get(brand);
	}

	public <C extends ElectricCar> C getElectricCar(String name){
		return (C)electricCars.get(name);
	}

	/*
		public <C extends ElectricCar> C getElectricCar(Car target){
			Optional<ElectricCar> optional = electricCars.values().stream().filter(electricCar ->
			{
				return electricCar.equals(target);
			}).findFirst();
			if(optional.isPresent()){
				return (C)optional.get();
			}
			return null;
		}
	*/
}
